<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.userId"
          size="mini"
          placeholder="请输入用户id"
          style="width:150px"
          clearable
        ></el-input>
        <el-select
          v-model="searchParams.type"
          placeholder="请选择类型"
          clearable
          size="mini"
          style="width:150px"
        >
          <el-option
            v-for="item in typeArr"
            :key="item.id"
            :value="item.id"
            :label="item.title"
          ></el-option>
        </el-select>
        <el-date-picker
          v-model="searchParams.startTime"
          type="datetime"
          placeholder="选择开始时间"
          size="mini"
        ></el-date-picker>
        <el-date-picker
          v-model="searchParams.endTime"
          type="datetime"
          placeholder="选择结束时间"
          size="mini"
        ></el-date-picker>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('userCommissions/index')"
          >搜索</el-button
        >
        <el-button
          type="primary"
          size="mini"
          @click="reset"
          v-if="has('userCommissions/index')"
          >重置</el-button
        >
      </el-row>
<!--      <el-row> 总充值：{{ data.total_recharge }} </el-row>-->
<!--      <el-row> 总兑换：{{ data.total_conversion }} </el-row>-->
<!--      <el-row> 总亏损：{{ data.total_loss }} </el-row>-->
      <el-table
        :data="data.data"
        v-loading="table_loading"
        row-key="id"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column
          label="收益用户"
          align="center"
          prop="user_id"
          width="200px"
        >
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-image
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px',
                  alignItems: 'center'
                }"
              ></el-image>
              <div
                style="display: flex;flex-direction: column;justify-content: space-between;"
              >
                <span>{{ scope.row.nickname }}</span>
                <span>ID:{{ scope.row.user_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="充值用户"
          align="center"
          prop="child_id"
          width="200px"
        >
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-image
                :src="scope.row.child_avatar"
                :preview-src-list="[scope.row.child_avatar]"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px',
                  alignItems: 'center'
                }"
              ></el-image>
              <div
                style="display: flex;flex-direction: column;justify-content: space-between;"
              >
                <span>{{ scope.row.child_nickname }}</span>
                <span>ID:{{ scope.row.child_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          prop="amount"
          label="流水"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="commission"
          label="收益"
          align="center"
        ></el-table-column>
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            <span style="color:green" v-if="scope.row.type == 1">无限赏</span>
            <span style="color:red" v-if="scope.row.type == 2">擂台赏</span>
            <span v-if="scope.row.type == 3">爬塔</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="领取" align="center">
          <template slot-scope="scope">
            <span style="color:green" v-if="scope.row.status == 1">是</span>
            <span style="color:red" v-if="scope.row.status == 0">否</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="updated_at"
          label="领取时间"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        page: 1,
        limit: 10,
        type: "",
        startTime: "",
        endTime: "",
        userId: ""
      },
      typeArr: [
        { id: 1, title: "无限赏" },
        { id: 2, title: "擂台赏" },
        { id: 3, title: "爬塔" },
      ]
    };
  },
  created() {
    this.getData();
  },

  methods: {
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.userCommissions
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.searchParams.userId = "";
      this.searchParams.startTime = "";
      this.searchParams.endTime = "";
      this.searchParams.type = "";
      this.addStatus = false;
      this.editStatus = false;
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    // 添加页面
    add() {
      this.addStatus = true;
    },
    // 编辑页面
    edit(id) {
      this.load();
      api.userCommissions
        .show(id)
        .then(res => {
          this.unload();
          if (res.data.code == 200) {
            this.editStatus = true;
            this.editParams = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 保存添加或编辑
    submit(formName, type) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          this.load();
          // 添加
          if (type === 1) {
            api.userCommissions
              .store(this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.getData();
                }
              })
              .catch(error => {
                console.log(error);
              });
          } else if (type == 2) {
            // 编辑
            api.userCommissions
              .edit(this.editParams.id, this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code == 200) {
                  this.getData();
                }
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    // 删除
    del(id, index) {
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          api.userCommissions
            .del(id)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.data.data.splice(index, 1);
                this.data.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
